package com.jzsm.modules.sender.service.impl;

import com.jzsm.common.utils.AssertUtils;
import com.jzsm.modules.sender.bo.message.MessageBo;
import com.jzsm.modules.sender.dao.PushChannelAccountDao;
import com.jzsm.modules.sender.dao.PushLogDao;
import com.jzsm.modules.sender.enums.ChannelTypeEnum;
import com.jzsm.modules.sender.holder.PushHolder;
import com.jzsm.modules.sender.pojo.PushChannelAccount;
import com.jzsm.modules.sender.pojo.PushLog;
import com.jzsm.modules.sender.service.IPushLogService;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@FieldDefaults(level = AccessLevel.PRIVATE)
@AllArgsConstructor
public class PushLogServiceImpl implements IPushLogService {
    final PushLogDao pushLogDao;
    final PushHolder pushHolder;
    final PushChannelAccountDao pushChannelAccountDao;

    @Override
    public PushLog selectById(String id) {
        AssertUtils.isNotBlank("id 参数不允许为空！", id);
        return pushLogDao.selectById(id);
    }

    @Override
    public List<PushLog> selectList(PushLog pushLog) {
        return pushLogDao.selectList(pushLog);
    }


    /**
     * 重发消息
     *
     * @param id
     * @return
     */
    @Override
    public String resendMessage(String id) {
        PushLog pushLog = pushLogDao.selectById(id);
        AssertUtils.isNotNull("未找到该日志信息！", pushLog);
        PushChannelAccount channelAccount = pushChannelAccountDao.selectById(pushLog.getChannelAccountId());
        AssertUtils.isNotNull("未找到渠道账号信息！", channelAccount);
        String code = channelAccount.getCode();
        ChannelTypeEnum channelTypeEnum = ChannelTypeEnum.valueOf(code);
        AssertUtils.isNotNull("该渠道不支持！", channelTypeEnum);
        String content = pushLog.getContent();
        MessageBo bo = new MessageBo();
        bo.setMessage(content).setAccount(channelAccount).setBusinessId(pushLog.getBusinessId()).setChannelType(channelTypeEnum);
        return pushHolder.pushToChannel(bo);
    }
}
